#include "HypothesisVerificationModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.hypothesis_verification_model.json";
}

HypothesisVerificationModel::HypothesisVerificationModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/global_hypothesis_verification.html");
}

HypothesisVerificationModel& HypothesisVerificationModel::get()
{
    static HypothesisVerificationModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void HypothesisVerificationModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    modelFile_ = json["modelFile"].toString(modelFile_);
    sceneFile_ = json["sceneFile"].toString(sceneFile_);

    showKeypoints_ = json["showKeypoints"].toBool(showKeypoints_);
    useHough_ = json["useHough"].toBool(useHough_);
    modelSamplingSize_ = json["modelSamplingSize"].toDouble(modelSamplingSize_);
    sceneSamplingSize_ = json["sceneSamplingSize"].toDouble(sceneSamplingSize_);
    lrfSupportRadius_ = json["lrfSupportRadius"].toDouble(lrfSupportRadius_);
    shotDescriptorRadius_ = json["shotDescriptorRadius"].toDouble(shotDescriptorRadius_);
    clusteringBinSize_ = json["clusteringBinSize"].toDouble(clusteringBinSize_);
    clusteringThresh_ = json["clusteringThresh"].toDouble(clusteringThresh_);

    icp_max_iter_ = json["icp_max_iter"].toInt(icp_max_iter_);
    icp_corr_distance_ = json["icp_corr_distance"].toDouble(icp_corr_distance_);
    hv_resolution_ = json["hv_resolution"].toDouble(hv_resolution_);
    hv_occupancy_grid_resolution_ = json["hv_occupancy_grid_resolution"].toDouble(hv_occupancy_grid_resolution_);
    hv_clutter_reg_ = json["hv_clutter_reg"].toDouble(hv_clutter_reg_);
    hv_inlier_th_ = json["hv_inlier_th"].toDouble(hv_inlier_th_);
    hv_occlusion_th_ = json["hv_occlusion_th"].toDouble(hv_occlusion_th_);
    hv_rad_clutter_ = json["hv_rad_clutter"].toDouble(hv_rad_clutter_);
    hv_regularizer_ = json["hv_regularizer"].toDouble(hv_regularizer_);
    hv_rad_normals_ = json["hv_rad_normals"].toDouble(hv_rad_normals_);
    hv_detect_clutter_ = json["hv_detect_clutter"].toBool(hv_detect_clutter_);
}

QJsonObject HypothesisVerificationModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("modelFile", QJsonValue(modelFile_));
    json.insert("sceneFile", QJsonValue(sceneFile_));
    json.insert("showKeypoints", QJsonValue(showKeypoints_));
    json.insert("useHough", QJsonValue(useHough_));
    json.insert("modelSamplingSize", QJsonValue(modelSamplingSize_));
    json.insert("sceneSamplingSize", QJsonValue(sceneSamplingSize_));
    json.insert("lrfSupportRadius", QJsonValue(lrfSupportRadius_));
    json.insert("shotDescriptorRadius", QJsonValue(shotDescriptorRadius_));
    json.insert("clusteringBinSize", QJsonValue(clusteringBinSize_));
    json.insert("clusteringThresh", QJsonValue(clusteringThresh_));

    json.insert("icp_max_iter", QJsonValue(icp_max_iter_));
    json.insert("icp_corr_distance", QJsonValue(icp_corr_distance_));
    json.insert("hv_resolution", QJsonValue(hv_resolution_));
    json.insert("hv_occupancy_grid_resolution", QJsonValue(hv_occupancy_grid_resolution_));
    json.insert("hv_clutter_reg", QJsonValue(hv_clutter_reg_));
    json.insert("hv_inlier_th", QJsonValue(hv_inlier_th_));
    json.insert("hv_occlusion_th", QJsonValue(hv_occlusion_th_));
    json.insert("hv_rad_clutter", QJsonValue(hv_rad_clutter_));
    json.insert("hv_regularizer", QJsonValue(hv_regularizer_));
    json.insert("hv_rad_normals", QJsonValue(hv_rad_normals_));
    json.insert("hv_detect_clutter", QJsonValue(hv_detect_clutter_));

    return json;
}
